clear all
set type double
set varabbrev on, perm

*** FILL IN MAIN PATH TO RUN CODE ***
global dropbox "..."

global folder "$dropbox/_0_bjps_replication"
global in "$folder/in"
global out "$folder/out"

global graph "$out/graph"
global table "$out/table"
global temp "`c(tmpdir)'"

set maxvar 120000


**************
*** hersh columns ***
u "$in/catalist_matched.dta", clear

g single_practice = docs_per_practice == 1

g rep_dr = 1 if partyaff == "REP"
replace rep_dr = 0 if partyaff == "DEM"

encode specialty, g (specialty_coded)
g female = npi_gender == "F"
g log_m_hh_income = log(medianhh)

g age_group = 0 if age < 18
replace age_group = 1 if inrange(age, 18,24)
replace age_group = 2 if inrange(age, 25, 34)
replace age_group = 3 if inrange(age, 35, 44)
replace age_group = 4 if inrange(age, 45, 54)
replace age_group = 5 if inrange(age, 55, 64)
replace age_group = 6 if inrange(age, 65, 74)
replace age_group = 7 if age > 74

label var age "Age"
label var female "Female"
label var single_practice "Single Practice"
label var log_m_hh_income "Block Median HH Income (log)"

**************************
*** for draft ***
eststo clear

eststo A: reg rep_dr single_practice, vce(robust)
	estadd local control = ""
	
eststo B: reghdfe rep_dr single_practice log_m_hh_income, absorb(i.age_group female i.specialty_coded) vce(robust)
	estadd local control = "X"
	
	
**************************
*** DIME columns ***
u "$in/matched_hpi3_md.dta", clear

g dollar_to_r = tot*pgr
bysort npi upin year:egen tot_to_r = total(dollar_to_r)
bysort npi upin year:egen tot_yr = total(tot)

isid npi upin year

g pct_to_r = tot_to_r/tot_yr

encode specialty_norm, g(specialty_encode)
encode gender, g(gender_encode)

destring earnings, replace ignore("NA")
g log_income = log(earnings)
replace log_income = 0 if mi(log_income)

destring gradyear, replace ignore("NA")
g yr_exp = year-gradyear
g yr_exp_cat = 0 if yr_exp < 0 | yr_exp > 70
replace yr_exp_cat = 1 if inrange(yr_exp,0,5)
replace yr_exp_cat = 2 if inrange(yr_exp, 6,10)
replace yr_exp_cat = 3 if inrange(yr_exp, 11,20)
replace yr_exp_cat = 4 if inrange(yr_exp, 21,30)
replace yr_exp_cat = 5 if inrange(yr_exp, 31,40)
replace yr_exp_cat = 6 if yr_exp > 40 & mi(yr_exp_cat)

drop if inlist(state, "AA", "AE", "GU", "PR", "AMERICAN SAMOA")
drop if inlist(state,"AP", "BERMUDA", "BRITISH COLUMBIA", "MB", "MP", "VI")
replace state = "FL" if state == "FLORIDA"
encode state, g(state_encode)

label variable single_practice "Single Practice"

*******
eststo C: reg pct_to_r single_practice, vce(robust)
	estadd local control = ""
	
eststo D: reghdfe pct_to_r single_practice, absorb(i.gender_encode i.state_encode i.yr_exp_cat i.specialty_encode) vce(robust)
	estadd local control = "X"
	
**** export table ****	
esttab A B C D using "$table/doctors.tex", replace ///
		cells(b(fmt(%-10.3fc) star) se(par fmt(%-10.3fc))) ///
		stats(control N r2, fmt(0 0 2)	///
		labels("Controls" "Observations" "R-squared" ))	///
		keep(single_practice _cons) ///
		mgroups("\% Republicans (Hersh)" "\% Donation to Republicans (DIME)" ,	///		
		pattern(1 0 1 0)		///		
		prefix(\multicolumn{@span}{c}{) suffix(}) span	) ///
		collabels(none) mlabels(none) label 

		
